package com.weavernorth.humanresource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import sun.misc.BASE64Encoder;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.io.IOException;
import java.util.Iterator;

import static com.weavernorth.humanresource.util.commonUtil.getGzdqCode;

/**
 * Created by guo_zy on 2019/12/31.
 * 调动
 */
public class personnelTransfer implements Action {
    public static Log log = LogFactory.getLog(personnelTransfer.class.getName());

    @Override
    public String execute(RequestInfo requestInfo) {
        //返回标识
        String fhz = "";
        boolean bolFlag = true;
        boolean isSuccess = false;
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        RecordSet rsSelect = new RecordSet();
        RecordSet mbrs = new RecordSet();
        RecordSet ssrs = new RecordSet();
        SalarRangeUtil salarRangeUtil = new SalarRangeUtil();
        //提示信息
        String strMessage = "";
        String requestid = requestInfo.getRequestid();//请求ID
        //String workflowid = requestInfo.getWorkflowid();//流程ID
        String tablename = requestInfo.getRequestManager().getBillTableName();//表单名称
        String ygbh = "";//员工编号
        String rzsj = "";//调动日期
        String czlx = "";//操作类型
        String czyy = "";//操作原因
        String gwmch = "";//岗位
        String rsfwh = "";//人事范围
        String rszfwh = "";//人事子范围
        String ygzh = "";//员工组
        String ygzzh = "";//员工子组
        String gzfwh = "";//工资核算范围
        String ssfzh = "";//税收分组
        String gzffgsh = "";//工资发放公司
        String sbjngsh = "";//社保缴纳公司
        String gjjjngsh = "";//公积金发放公司
        String xzlxh = "";//薪资类型
        String xzqyh = "";//薪资区域
        String xzgjbh = "";//薪资岗级别
        String xzddch = "";//薪资岗档次
        String jbgzh = "";//基本工资
        String jxgzh = "";//绩效工资
        String nzjjjsh = "";//年终奖金基数
        String sbjsh = "";//社保基数
        String sbjnrqh = "";//社保缴纳日期
        String gjjjsh = "";//公积金基数
        String gjjjnrqh = "";//公积金缴纳日期
        //社保缴纳地
        String strSbjnd = "";
        //公积金缴纳地
        String strGjjjnd = "";
        String strGzdq="";
        String strJglx="";
        String strYglx="";
        String sql = "select gzdq,jglx,yglx,gjjjndq,ygbh,rzsj,czlx,czyy,gwmch,rsfwh,rszfwh,ygzh,ygzzh,gzfwh,ssfzz,gzffgsh,sbjngsh,gjjjngsh,xzlxh,xzqyh,xzgjbh,xzddch,jbgzh,jxgzh,nzjjjsh,sbjsh,sbjnrqh,gjjjsh,gjjjnrqh,sbjndq from " + tablename + " where requestid =" + requestid;
        rs.execute(sql);
        log.info("查询当前调动信息:" + sql);
        if (rs.getCounts() != 1) {
            return FAILURE_AND_CONTINUE;
        }
        while (rs.next()) {
            ygbh = rs.getString("ygbh");//员工编号
            rzsj = rs.getString("rzsj");//调动日期
            czlx = getczlx(rs.getString("czlx"));//操作类型
            czyy = getczyy(rs.getString("czyy"));//操作原因
            gwmch = rs.getString("gwmch");//岗位
            rsfwh = rs.getString("rsfwh");//人事范围
            rszfwh = rs.getString("rszfwh");//人事子范围
            ygzh = rs.getString("ygzh");//员工组
            ygzzh = rs.getString("ygzzh");//员工子组
            gzfwh = rs.getString("gzfwh");//工资核算范围
            ssfzh = rs.getString("ssfzz");//税收分组
            gzffgsh = rs.getString("gzffgsh");//工资发放公司
            sbjngsh = rs.getString("sbjngsh");//社保缴纳公司
            gjjjngsh = rs.getString("gjjjngsh");//公积金发放公司
            xzlxh = getxzlxh(rs.getString("xzlxh"));//薪资类型
            xzqyh = getxzqyh(rs.getString("xzqyh"));//薪资区域
            xzgjbh = rs.getString("xzgjbh");//薪资岗级别
            xzddch = rs.getString("xzddch");//薪资岗档次
            jbgzh = rs.getString("jbgzh");//基本工资
            if("".equals(jbgzh)){
                jbgzh="0.00";
            }
            jxgzh = rs.getString("jxgzh");//绩效工资
            if("".equals(jxgzh)){
                jxgzh="0.00";
            }
            nzjjjsh = rs.getString("nzjjjsh");//年终奖金基数
            if("".equals(nzjjjsh)){
                nzjjjsh="0.00";
            }
            sbjsh = rs.getString("sbjsh");//社保基数
            if("".equals(sbjsh)){
                sbjsh="0.00";
            }
            sbjnrqh = rs.getString("sbjnrqh");//社保缴纳日期
            gjjjsh = rs.getString("gjjjsh");//公积金基数
            if("".equals(gjjjsh)){
                gjjjsh="0.00";
            }
            gjjjnrqh = rs.getString("gjjjnrqh");//公积金缴纳日期
            //公积金缴纳地
            strGjjjnd = rs.getString("gjjjndq");
            //社保缴纳地
            strSbjnd = rs.getString("sbjndq");
            strGzdq=getGzdqCode(Util.null2String(rs.getString("gzdq")));
            strJglx=Util.null2String(rs.getString("jglx"));
            strYglx=Util.null2String(rs.getString("yglx"));
        }
        sql = "select jobtitlecode from hrmjobtitles where id = " + gwmch;
        rs.execute(sql);
        while (rs.next()) {
            gwmch = rs.getString("jobtitlecode");//岗位
        }
        sql = "select rsfwdm from uf_rsfw where id = " + rsfwh;
        rs.execute(sql);
        while (rs.next()) {
            rsfwh = rs.getString("rsfwdm");//人事范围
        }
        sql = "select rszfwdm from uf_rszfw where id = " + rszfwh;
        rs.execute(sql);
        while (rs.next()) {
            rszfwh = rs.getString("rszfwdm");//人事子范围
        }
        sql = "select ygzdm from uf_ygz where id = " + ygzh;
        rs.execute(sql);
        while (rs.next()) {
            ygzh = rs.getString("ygzdm");//员工组
        }
        sql = "select ygzzdm from uf_ygzz where id = " + ygzzh;
        rs.execute(sql);
        while (rs.next()) {
            ygzzh = rs.getString("ygzzdm");//员工子组
        }
        sql = "select gzhsfw from uf_gzfw where id = " + gzfwh;
        rs.execute(sql);
        while (rs.next()) {
            gzfwh = rs.getString("gzhsfw");//工资核算范围
        }

        //zm 增加,判断工资范围是否锁定 开始

        String strLockMess = salarRangeUtil.judgeLock(gzfwh, rzsj);
        log.info("工资核算范围返回信息:" + strLockMess);
        if (!"".equals(strLockMess)) {
            strLockMess = strLockMess + "薪资核算结束后，工资范围解除锁定，流程可以继续提交";
            requestInfo.getRequestManager().setMessagecontent(strLockMess);
            return FAILURE_AND_CONTINUE;

        }
        //zm 增加,判断工资范围是否锁定 结束


        // sql = "select rsfwdm from uf_rsfw where id = " + ssfzh;
        String ssSql = "select erssfz from uf_ssfzmb where id = " + ssfzh;
        ssrs.execute(ssSql);
        while (ssrs.next()) {
            ssfzh = ssrs.getString("erssfz");//税收分组
        }
        sql = "select selectname from workflow_selectitem where fieldid=15397 and selectvalue=" + xzgjbh;
        rs.execute(sql);
        while (rs.next()) {
            xzgjbh = rs.getString("selectname");//薪资岗级别
        }
        sql = "select selectname from workflow_selectitem where fieldid=15398 and selectvalue=" + xzddch;
        rs.execute(sql);
        while (rs.next()) {
            xzddch = rs.getString("selectname");//薪资岗档次
        }

        StringBuffer xml = new StringBuffer("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:sap-com:document:sap:soap:functions:mc-style\">");
        xml.append("<soapenv:Header/><soapenv:Body><urn:Zhri073>");
        xml.append("<IAbkrs>" + gzfwh + "</IAbkrs>");
        xml.append("<IBegda>" + rzsj + "</IBegda>");
        xml.append("<IBegda2>" + sbjnrqh + "</IBegda2>");
        xml.append("<IBegda3>" + gjjjnrqh + "</IBegda3>");
        xml.append("<IBet01>" + jbgzh + "</IBet01>");
        xml.append("<IBet02>" + jxgzh + "</IBet02>");
        xml.append("<IBet03>" + nzjjjsh + "</IBet03>");
        xml.append("<IBet04>" + sbjsh + "</IBet04>");
        xml.append("<IBet05>" + gjjjsh + "</IBet05>");
        xml.append("<IBtrtl>" + rszfwh + "</IBtrtl>");
        xml.append("<IMassg>" + czyy + "</IMassg>");
        xml.append("<IMassn>" + czlx + "</IMassn>");
        xml.append("<IPernr>" + ygbh + "</IPernr>");
        xml.append("<IPersg>" + ygzh + "</IPersg>");
        xml.append("<IPersk>" + ygzzh + "</IPersk>");
        xml.append("<IPlans>" + gwmch + "</IPlans>");
        xml.append("<ITaxgp>" + ssfzh + "</ITaxgp>");
        xml.append("<ITrfar>" + xzlxh + "</ITrfar>");
        xml.append("<ITrfgb>" + xzqyh + "</ITrfgb>");
        xml.append("<ITrfgr>" + xzgjbh + "</ITrfgr>");
        xml.append("<ITrfst>" + xzddch + "</ITrfst>");
        xml.append("<IWerks>" + rsfwh + "</IWerks>");
        xml.append("<IZgjj>" + sbjngsh + "</IZgjj>");
        xml.append("<IZgz>" + gzffgsh + "</IZgz>");
        //region[====新增字段=====]
        xml.append("<IZgzdq>"+strGzdq+"</IZgzdq>");
        xml.append("<IZjglx>"+strJglx+"</IZjglx>");
        xml.append("<IZyglx>"+strYglx+"</IZyglx>");
        //endregion
        xml.append("<IZsb>" + gjjjngsh + "</IZsb>");
        xml.append("<Return><item>");
        xml.append("<Type></Type><Id></Id><Number></Number><Message></Message>");
        xml.append("<LogNo></LogNo><LogMsgNo></LogMsgNo><MessageV1></MessageV1>");
        xml.append("<MessageV2></MessageV2><MessageV3></MessageV3><MessageV4></MessageV4>");
        xml.append("<Parameter></Parameter><Row></Row><Field></Field><System></System>");
        xml.append("</item></Return>");

        //****************社保码表转码*************************

        String sbjnddm = ""; //社保缴纳地代码
        String mbsql = "select  jnddm  from  uf_sbmb  where id = '" + strSbjnd + "'";
        mbrs.execute(mbsql);
        if (mbrs.next()) {
            sbjnddm = mbrs.getString("jnddm");
        }


        //养老社保基数
        String strYl = "";
        //失业社保基数
        String strSy = "";
        //医疗
        String strYil = "";
        //工商
        String strGs = "";
        //孕产
        String strYunC = "";

        //公积金结构
        StringBuffer stringBuffer = new StringBuffer();

        xml.append("<T0532>");
        stringBuffer.append("<T0530>");
        //查询明细表数据推送
        String detailSql = "select fmd.* from " + tablename + " fm ," + tablename + "_dt1 fmd where fm.id=fmd.mainid and fm.requestid=? ";
        log.info("查询明细表社保基数:" + detailSql + ",requestid:" + requestid);
        rsSelect.executeQuery(detailSql, requestid);
        while (rsSelect.next()) {
            int bxlx = rsSelect.getInt("BXLX");
            log.info("报销类型:" + bxlx);
            //社保基数
            String strSbjs = Util.null2String(rsSelect.getString("SBJS"));
            log.info("社保基数:" + strSbjs);
            if (bxlx == 0) {
                strYl = strSbjs;
            } else if (bxlx == 1) {
                strSy = strSbjs;
            } else if (bxlx == 2) {
                strYil = strSbjs;
            } else if (bxlx == 3) {
                strGs = strSbjs;
            } else if (bxlx == 4) {
                strYunC = strSbjs;
            }


            //保险代码
            String bxlxdm = rsSelect.getString("BXLXDM");
            //缴纳方式
            int jnfs = Util.getIntValue(rsSelect.getString("JNFS"), 0);
            //****************行业XY转码**************************
            String xy = ""; //行业
            //根据字段名称得到fieldid
            String strXyFieldid = salarRangeUtil.getFieldid(tablename, tablename + "_dt1", "xy");
            //得到对应的值
            xy = salarRangeUtil.getFieldValue(strXyFieldid, Util.null2String(rsSelect.getString("XY")));
//            switch (rsSelect.getInt("XY")) {
//
//                case 0:
//                    xy = "01";
//                    break;
//                case 1:
//                    xy = "02";
//                    break;
//                case 2:
//                    xy = "03";
//                    break;
//                case 3:
//                    xy = "04";
//                    break;
//                case 4:
//                    xy = "16";
//                    break;
//            }

            bb.writeLog("行业xy的值:" + xy + "\n");

            //******************************************

            //****************组Z转码**************************
            String z = ""; //组
            //根据字段名称得到fieldid
            String strZFieldid = salarRangeUtil.getFieldid(tablename, tablename + "_dt1", "z");
            //得到对应的值
            z = salarRangeUtil.getFieldValue(strZFieldid, Util.null2String(rsSelect.getString("Z")));
//            switch (rsSelect.getInt("Z")) {
//                case 0:
//                    z = "GR01";
//                    break;
//                case 1:
//                    z = "GR02";
//                    break;
//            }

            bb.writeLog("组z的值:" + z + "\n");

            //******************************************


            //****************级别jb转码**************************
            String jb = ""; //级别
            //根据字段名称得到fieldid
            String strJbFieldid = salarRangeUtil.getFieldid(tablename, tablename + "_dt1", "jb");
            //得到对应的值
            jb = salarRangeUtil.getFieldValue(strJbFieldid, Util.null2String(rsSelect.getString("JB")));
            bb.writeLog("级别jb的值:" + jb + "\n");
            //缴纳方式
            String strJnfs = "";
            switch (jnfs) {
                case 0:
                    strJnfs = "1";
                    break;
                case 1:
                    strJnfs = "2";
                    break;
                case 2:
                    strJnfs = "3";
                    break;
                case 3:
                    strJnfs = "4";
                    break;
            }


            //******************************************
            if (bxlx != 5) {
                xml.append("<item>");
                xml.append("<Begdas>" + sbjnrqh + "</Begdas>");    //社保缴纳日期
                xml.append("<Conar>" + sbjnddm + "</Conar>");    //社保缴纳地
                xml.append("<Intyp>" + bxlxdm + "</Intyp>");     //保险类型代码
                xml.append("<Conty1>" + xy + "</Conty1>");    //行业
                xml.append("<Congr1>" + z + "</Congr1>");     //组
                xml.append("<Conlv1>" + jb + "</Conlv1>");   //级别
                xml.append("<Pbyer>" + strJnfs + "</Pbyer>");

                xml.append("</item>");
            } else {
                String gjjjnddm = ""; //公积金缴纳地代码
                mbsql = "select  jnddm  from  uf_gjjmb  where id = '" + strGjjjnd + "'";
                mbrs.execute(mbsql);
                if (mbrs.next()) {
                    gjjjnddm = mbrs.getString("jnddm");
                }


                stringBuffer.append("<item>");
                stringBuffer.append("<Pegdap>" + gjjjnrqh + "</Pegdap>");
                stringBuffer.append("<Conarp>" + gjjjnddm + "</Conarp>");
                stringBuffer.append(" <Conty2>" + xy + "</Conty2>");
                stringBuffer.append(" <Congr2>" + z + "</Congr2>");
                stringBuffer.append(" <Conlv2>" + jb + "</Conlv2>");
                stringBuffer.append("<Pbyer>" + strJnfs + "</Pbyer>");
                stringBuffer.append("</item>");

            }
        }
        xml.append("</T0532>");
        stringBuffer.append("</T0530>");
        xml.append(stringBuffer);


        /**社保基数*/
        xml.append("<TSbjs>");
        xml.append("<item>");
        xml.append(" <Zzyanglaojs>" + strYl + "</Zzyanglaojs>");
        xml.append("<Zzyiliaojs>" + strYil + "</Zzyiliaojs>");
        xml.append(" <Zzshiyejs>" + strSy + "</Zzshiyejs>");
        xml.append("<Zzgongshangjs>" + strGs + "</Zzgongshangjs>");
        xml.append("<Zzshengyujs>" + strYunC + "</Zzshengyujs>");
        xml.append("</item>");
        xml.append("</TSbjs>");
        /**社保基数*/


        xml.append("</urn:Zhri073></soapenv:Body></soapenv:Envelope>");
        log.info("diaodongchuanshudexmlis:" + xml.toString());
        StringBuffer strbuMess = new StringBuffer();
        try {
            String ddUrl = bb.getPropValue("personnel-erdos", "ddurl");//获取配置文件信息
            String responseXml = salarRangeUtil.sendPost(ddUrl, xml.toString(), "utf-8");
            log.info("diaodongfanhuizhi:" + responseXml);//打印日志
            Document doc = null;//解析返回值开始
            int intType = 0;


            try {
                doc = DocumentHelper.parseText(responseXml);
                Iterator<Element> its2 = doc.selectNodes("//Type").iterator();
                Iterator<Element> its3 = doc.selectNodes("//Message").iterator();
                while (its2.hasNext() && its3.hasNext()) {
                    Element element = its2.next();
                    Element element2 = its3.next();
                    String testType = element.getText();
                    String strMess = element2.getText();
                    log.info("返回的类型:"+testType+",返回的信息:"+strMess);

                    if (!"".equals(testType) && "S".equals(testType)) {
                        isSuccess = true;
                       log.info("testType值为:" + testType + "\n");
                       break;

                    }
                    if (!"".equals(testType) && "E".equals(testType)) {
                        strbuMess.append(strMess);
                    }

                }


            } catch (Exception e) {
                e.printStackTrace();
            }


//            try {
//                doc = DocumentHelper.parseText(responseXml);
//                Iterator<Element> its2 = doc.selectNodes("//Type").iterator();
//                while (its2.hasNext()) {
//                    Element element = its2.next();
//                    fhz = element.getText();
//                    if ("E".equals(fhz)) {
//                        bolFlag=false;
//                        break;
//                    }
//                    intType++;
//                }
//
//                int intMess = 0;
//
//                //返回值失败时,则提示失败
//                if ("E".equals(fhz)) {
//                    Iterator<Element> its3 = doc.selectNodes("//Message").iterator();
//                    while (its3.hasNext()) {
//                        Element element2 = its3.next();
//                        strMessage = element2.getText();
//                        log.info("intMess:" + intMess + ",intType:" + intType);
//                        if (intMess == intType) {
//                            if (!"".equals(strMessage)) {
//                                break;
//                            }
//                        }
//
//                        intMess++;
//                    }
//
//                }
//
//            } catch (DocumentException e) {
//                e.printStackTrace();
//            }//解析结束
        } catch (Exception e) {
            e.printStackTrace();
        }

        log.info("当前返回状态:"+isSuccess);
        if (isSuccess) {
            return SUCCESS;
        } else {
            log.info("当前报错信息:"+strbuMess);
            if (strbuMess!=null&&(strbuMess.toString()).length()>0) {
                requestInfo.getRequestManager().setMessagecontent(strbuMess.toString());
            }
            return FAILURE_AND_CONTINUE;
        }
    }


    private String getczlx(String tj) {
        String jg = "";
        switch (tj) {
            case "0":
                jg = "Z3";
                break;
            case "1":
                jg = "Z6";
                break;
            case "2":
                jg = "Z7";
                break;
            case "3":
                jg = "Z8";
                break;
            case "4":
                jg = "ZE";
                break;
            case "5":
                jg = "ZG";
                break;
            case "6":
                jg = "ZK";
                break;
            case "7":
                jg = "ZN";
                break;
            case "8":
                jg = "ZP";
                break;
            default:
                jg = "";
        }
        return jg;
    }

    private String getczyy(String tj) {
        String jg = "";
        switch (tj) {
            case "0":
                jg = "10";
                break;
            case "1":
                jg = "20";
                break;
            case "2":
                jg = "30";
                break;
            case "3":
                jg = "40";
                break;
            case "4":
                jg = "50";
                break;
            case "5":
                jg = "10";
                break;
            case "6":
                jg = "10";
                break;
            case "7":
                jg = "10";
                break;
            case "8":
                jg = "15";
                break;
            case "9":
                jg = "20";
                break;
            case "10":
                jg = "10";
                break;
            default:
                jg = "";
        }
        return jg;
    }

    private String getxzlxh(String tj) {
        String jg = "";
        switch (tj) {
            case "0":
                jg = "10";
                break;
            default:
                jg = "";
        }
        return jg;
    }

    private String getxzqyh(String tj) {
        String jg = "";
        switch (tj) {
            case "0":
                jg = "10";
                break;
            case "1":
                jg = "20";
                break;
            case "2":
                jg = "30";
                break;
            default:
                jg = "";
        }
        return jg;
    }
}
